package com.mjl.shops.utils;

import com.mjl.shops.bean.CollatorRightBean;
import com.mjl.shops.bean.CollectionBean;
import com.mjl.shops.bean.ConvertBean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class JsonUtils {

    public static List<CollatorRightBean> paresCollator(JSONObject jsonObject) {

        List<CollatorRightBean> collatorRightBeans = new ArrayList<>();
        CollatorRightBean titleCollatorRightBean;
        CollatorRightBean goodsCollatorRightBean;
        CollatorRightBean.TitleName titleName;
        CollatorRightBean.Goods goods;

        JSONArray jg = jsonObject.optJSONArray("goods");
        if (jg != null && jg.length() > 0) {
            for (int i = 0; i < jg.length(); i++) {
                JSONObject jt = jg.optJSONObject(i);
                titleName = new CollatorRightBean.TitleName();
                titleName.setTitle(jt.optString("title"));
                titleName.setIsAll(jt.optInt("isAll"));
                titleCollatorRightBean = new CollatorRightBean();
                titleCollatorRightBean.setTitleName(titleName);
                titleCollatorRightBean.setItemType(CollatorRightBean.TITLE);
                collatorRightBeans.add(titleCollatorRightBean);

                JSONArray jgs = jt.optJSONArray("good");
                if (jgs != null && jgs.length() > 0) {
                    for (int x = 0; x < jgs.length(); x++) {
                        JSONObject js = jgs.optJSONObject(x);
                        goodsCollatorRightBean = new CollatorRightBean();
                        goods = new CollatorRightBean.Goods();
                        goods.setId(js.optString("id"));
                        goods.setName(js.optString("name"));
                        goods.setImg(js.optString("img"));
                        goods.setPrice(js.optDouble("price"));
                        goods.setAddNum(js.optInt("addNum"));
                        goodsCollatorRightBean.setGoods(goods);
                        goodsCollatorRightBean.setItemType(CollatorRightBean.GOOD);
                        collatorRightBeans.add(goodsCollatorRightBean);
                    }
                }

            }
        }

        return collatorRightBeans;
    }

    public static List<CollectionBean> paresCollectionBean(JSONObject jsonObject) {
        List<CollectionBean> collectionBeans = new ArrayList<>();
        CollectionBean collectionBean;
        JSONArray jsArray = jsonObject.optJSONArray("collection");
        if (jsArray != null && jsArray.length() > 0) {
            for (int i = 0; i < jsArray.length(); i++) {
                JSONObject jo = jsArray.optJSONObject(i);
                collectionBean = new CollectionBean();
                collectionBean.setGoodsId(jo.optString("goodsId"));
                collectionBean.setUrl(jo.optString("url"));
                collectionBean.setName(jo.optString("name"));
                collectionBean.setPrice(jo.optDouble("price"));
                collectionBean.setPay_person_num(jo.optInt("pay_person_num"));
                collectionBean.setIsSelect(jo.optInt("isSelect"));
                collectionBeans.add(collectionBean);
            }
        }
        return collectionBeans;
    }

    public static List<ConvertBean> paresConvertList(JSONObject jsonObject) {

        List<ConvertBean> convertBeans = new ArrayList<>();
        ConvertBean convertBean;

        JSONArray jc = jsonObject.optJSONArray("convert");
        if (jc != null && jc.length() > 0) {
            for (int i = 0; i < jc.length(); i++) {
                JSONObject jb = jc.optJSONObject(i);
                convertBean = new ConvertBean();
                convertBean.setId(jb.optString("id"));
                convertBean.setActivityName(jb.optString("activity_name"));
                convertBean.setTicketCode(jb.optString("ticket_code"));
                convertBean.setConverDate(jb.optString("conver_date"));
                convertBean.setConsigneePerson(jb.optString("consignee_person"));
                convertBean.setConsigneeAddress(jb.optString("consignee_address"));
                convertBean.setConsigneeState(jb.optString("consignee_state"));
                convertBean.setExpressCode(jb.optString("express_code"));
                convertBean.setExpressCompany(jb.optString("express_company"));
                convertBeans.add(convertBean);
            }
        }

        return convertBeans;
    }

}
